package cn.enjoy.sharding.controller;

import cn.enjoy.sharding.entity.HssHistoryEntity;
import cn.enjoy.sharding.service.HssHistoryService;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ShardingController {
    @Resource
    private HssHistoryService hssHistoryService;


    @PostMapping("/insert")
    public String insert(@RequestBody HssHistoryEntity entity) {
//        entity.setCreateTime(DateUtil.offsetMonth(new Date(), 1));
        entity.setCreateTime(DateUtil.date());
        hssHistoryService.save(entity);
        return "成功";
    }

    @PostMapping("/getById")
    public HssHistoryEntity getById(@RequestBody HssHistoryEntity entity) {
        DateTime beginOfMonth = DateUtil.beginOfMonth(DateUtil.date());
        DateTime endOfMonth = DateUtil.endOfMonth(DateUtil.date());
        LambdaQueryWrapper<HssHistoryEntity> lambdaQueryWrapper = Wrappers.<HssHistoryEntity>lambdaQuery()
                .ge(HssHistoryEntity::getCreateTime, beginOfMonth)
                .le(HssHistoryEntity::getCreateTime, endOfMonth)
                .eq(HssHistoryEntity::getEquipmentId, entity.getEquipmentId());

        HssHistoryEntity one = hssHistoryService.getOne(lambdaQueryWrapper);
        return one;
    }
}
